package com.it.test6case2;

import java.util.ArrayList;
import java.util.Collections;

public class MyThread extends Thread {
    //共享数据
    //用集合存储数据，利于删除数据
    //在成员位置定义集合
    ArrayList<Integer> list;
    //补一个构造方法
    public MyThread(ArrayList<Integer> list) {
        this.list = list;
    }

    public void run() {
        ArrayList<Integer> boxlist = new ArrayList<>();
        while (true) {
            synchronized (MyThread.class) {
                if (list.size() == 0) {
                    if("抽奖箱1".equals(getName())) {
                        System.out.println("抽奖箱1"+boxlist);
                    }else{
                        System.out.println("抽奖箱2"+boxlist);
                    }
                    break;
                }else{
                    Collections.shuffle(list);
                    int prize = list.remove(0);
                    if("抽奖箱1".equals(getName())) {
                        boxlist.add(prize);
                    }else{
                        boxlist.add(prize);
                    }
                }
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
